package org.example.demo1.servlets.ManagePage;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebServlet("/API/user/logout")
public class LogoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取当前会话，如果没有则返回null
        HttpSession session = req.getSession(false);
        if (session != null) {
            // 使会话无效
            session.invalidate();
        }
        // 重定向到登录页面
        resp.sendRedirect(req.getContextPath() + "/login.jsp");
    }
}